package Query.SearchEngine;

import java.util.Vector;
import Database.Document;
import Query.*;
import java.io.*;
import java.util.*;

public class LiveSearchEngineAdapter implements IEngineAdapter {
    
    public final static String PATH = System.getProperty("user.dir") + "\\LiveSearchWebServiceClient.exe";
    
    public LiveSearchEngineAdapter() {
    }
    
    public Vector<Document> Query(String query, int length)
    {
        Vector<Document> result = new Vector<Document>();
        try 
        {
            String line;
            Process p = Runtime.getRuntime().exec(PATH + " \"" + query + "\"" + " " + length);
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) 
            {
                StringTokenizer str = new StringTokenizer(line, "|");
                String[] temp = new String[3]; 
                temp[0] = (String)str.nextElement();
                temp[1] = (String)str.nextElement();
                temp[2] = (String)str.nextElement();
                result.add(new Document(temp[2].trim(), temp[0].trim(), temp[1], this.getSearchEngine()));
            }
            input.close();
            return result;
        }
        catch (Exception err)
        {
            err.printStackTrace();
            return null;
        }
    }
    
    public SearchEnginesTypes getSearchEngine()
    {
        return SearchEnginesTypes.LIVE_SEARCH;
    }
}
